import 'package:flutter/material.dart';
import 'package:english_words/english_words.dart';

class NewRoute extends StatefulWidget {
  @override
  _ScaffoldRouteState createState() => _ScaffoldRouteState();
}

class _ScaffoldRouteState extends State<NewRoute> {
  
  ScrollController _controller = new ScrollController();
  bool showToTopBtn = false;
  int _selectedIndex = 0;
  List tabs = ["新闻", "历史", "图片"];
  String str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  static const loadingTag = "##loading##"; //表尾标记
  var _words = <String>[loadingTag];

  void _onItemTapped(int index) {
    setState(() {
      _selectedIndex = index;
    });
  }

  @override
  void initState() {
    super.initState();
    _controller.addListener(() {
      print(_controller.offset);
      if(_controller.offset < 1000 && showToTopBtn) {
        this.setState((){
          showToTopBtn = false;
        });
      } else if(_controller.offset >= 1000 && !showToTopBtn) {
        this.setState(() {
          showToTopBtn = true;
        });
      }
    });
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Home'),
        actions: <Widget>[
          IconButton(icon: Icon(Icons.share), onPressed: () {
            
          },)
        ],
      ),
      bottomNavigationBar: BottomNavigationBar(
        items: <BottomNavigationBarItem>[
          BottomNavigationBarItem(icon: Icon(Icons.home), title: Text('Home')),
          BottomNavigationBarItem(icon: Icon(Icons.school), title: Text('School')),
          BottomNavigationBarItem(icon: Icon(Icons.settings), title: Text('Setting'))
        ],
        currentIndex: _selectedIndex,
        fixedColor: Colors.orange,
        onTap: _onItemTapped,
      ),
      body: Column(
        children: <Widget>[
          ListTile(title: Text('商品列表')),
          Expanded(
            child: ListView.separated(
              itemCount: _words.length,
              itemBuilder: (context, index) {
                if(_words[index] == loadingTag) {
                  if(_words.length - 1 < 100) {
                    _retrieveData();
                    return Container(
                      padding: const EdgeInsets.all(16.0),
                      alignment: Alignment.center,
                      child: SizedBox(
                          width: 24.0,
                          height: 24.0,
                          child: CircularProgressIndicator(strokeWidth: 2.0)
                      ),
                    );
                  } else {
                    return Container(
                      alignment: Alignment.center,
                      padding: const EdgeInsets.all(16.0),
                      child: Text('没有更多了', style: TextStyle(color: Colors.grey)),
                    );
                  }
                }
                return ListTile(title: Text(_words[index]),);
              }, 
              separatorBuilder: (context, index) {
                return Divider(color: Colors.black54, height: 0.5,);
              },
            ),
          )
        ],
      ),
      floatingActionButton: !showToTopBtn ? null : FloatingActionButton(
        child: Icon(Icons.arrow_upward),
        onPressed: () {
          _controller.animateTo(.0, duration: Duration(milliseconds: 200), curve: Curves.ease);
        },
      )
    );
  }
  void _retrieveData() {
    Future.delayed(Duration(seconds: 1)).then((e) {
      _words.insertAll(_words.length - 1,
          //每次生成20个单词
          generateWordPairs().take(20).map((e) => e.asPascalCase).toList()
      );
      setState(() {
        //重新构建列表
      });
    });
  }
}